const WXAPI = require('../../../wxapi/main')
const con = require("../../../utils/data.js");
const CONFIG = require('../../../config.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    promoCode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.setData({
      promoCode: options.promoCode,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindGetUserInfo: function(e) {
    if (!e.detail.userInfo) {
      return;
    }
    if (app.globalData.isConnected) {
      wx.setStorageSync('userInfo', e.detail.userInfo)
      this.bindSpread();
    } else {
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  },
  //用code请求openid
  bindSpread: function() {
    var that = this;
    wx.login({
      success: function(res) {
        var code = res.code; //返回code
        console.log(code);
        wx.request({
          url: 'https://www.brvpmusic.com/wechat/getOpenId',
          data: {
            'code': code
          },
          method: 'GET',
          header: {
            'content-type': 'json'
          },
          success: function(res) {
            var userInfo = wx.getStorageSync('userInfo');
            var openid = res.data.openid; //返回openid
            var session_key = res.data.session_key; //返回session_key
            // 存用户信息到全局变量
            var app = getApp();
            app.globalData.session_key = session_key;
            if (openid != '' && openid != 'undefined') {
              app.globalData.openid = openid;
              wx.setStorageSync('OpenId', openid);
            } else {
              app.globalData.openid = '';
              wx.setStorageSync('OpenId', '');
            }
            console.log('返回成功');
            console.log(userInfo.nickName);
            console.log('返回openid---' + openid);
            console.log(userInfo.avatarUrl);
            console.log("结果-promoCode----" + that.data.promoCode);
            // 触发注册openid
            wx.request({
              url: 'https://www.brvpmusic.com/wechat/bindSpread',
              method: 'GET',
              header: {
                'content-type': 'application/json'
              },
              data: {
                'openid': app.globalData.openid,
                'nickname': userInfo.nickName,
                'cHeadimgurl': userInfo.avatarUrl,
                'promoCode': that.data.promoCode
              },
              success: function(res) {
                console.log('openid，昵称，头像传输成功');
                console.log(res.data);
                if (res.data.code == 200) {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 3000,
                    success: function() {
                      setTimeout(() => {
                        // 已经处于登录状态，检测是否强制需要手机号码
                        var phoneNumber = wx.getStorageSync('phoneNumber');
                        console.log("phoneNumber:"+phoneNumber);
                        if(phoneNumber !='' && phoneNumber!=null&& typeof (phoneNumber)!='undefined') {
                          wx.reLaunch({
                            url: "/pages/index/index"
                          })
                        }else{
                                wx.navigateTo({
                                  url: "/pages/authorize/bindmobile"
                                })
                        }
                      }, 1000);
                    }
                  });
                } else {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 1500
                  });
                }
              },
              fail: function(res) {
                console.log('openid，昵称，头像传输失败')
              }
            })
          }
        })
      }
    })
  }
})